package com.lubase.wfengine.consumer.rabbit;

import com.alibaba.fastjson.JSON;
import com.lubase.wfengine.auto.entity.WfCallbackEntity;
import com.lubase.wfengine.auto.entity.WfTEventEntity;
import com.lubase.wfengine.consumer.ProcessEngineEventService;
import com.lubase.wfengine.consumer.UpdateBisTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@ConditionalOnProperty(name = "lubase.wf-engine.mq-type", havingValue = "rabbitmq")
public class RabbitMQConsumerService {
    @Autowired
    UpdateBisTableService updateBisTableService;

    @RabbitListener(queues = "${lubase.wf-engine.update-table-topic:LUBASE_WF_UPDATE_TABLE_TOPIC}")
    public void onMessage1(String message) {
        WfCallbackEntity entity = null;
        try {
            entity = JSON.parseObject(message, WfCallbackEntity.class);
            updateBisTableService.updateBisTable(entity);
        } catch (Exception exception) {
            log.error("processWftEvent 执行失败，event id is" + message, exception);
        }
    }

    @Autowired
    ProcessEngineEventService processEngineEventService;

    @RabbitListener(queues = "${lubase.wf-engine.engine-topic:LUBASE_WF_ENGINE_TOPIC}")
    public void onMessage2(String message) {
        WfTEventEntity entity = null;
        try {
            entity = JSON.parseObject(message, WfTEventEntity.class);
            processEngineEventService.processWftEvent(entity);
        } catch (Exception exception) {
            log.error("processWftEvent 执行失败，event id is" + message, exception);
        }
    }
}
